

# 导包
import json
from pyecharts.charts import Map
from pyecharts.options import TitleOpts,VisualMapOpts

# 读取文件
with open("D:/python-learn/data/疫情.txt", "r", encoding="UTF-8") as f:
    data = f.read()

# 将JSON文件转换为字典
data_dict = json.loads(data)

# 取出所需数据
canton_data = data_dict['areaTree'][0]['children'][7]['children']

canton_data_list = []
# 取出各城市名字与确诊数据
# def canton_data_list_(canton_data):
#     city_name = []
#     city_confirm = []
#     for canton_city_data in canton_data:
#         canton_city_name = canton_city_data['name']                     # 获取城市名字
#         canton_city_confirm = canton_city_data['total']['confirm']      # 获取城市确诊数据
#         if canton_city_name == "境外输入" or canton_city_name == "地区待确认":
#             continue
#         else:
#             canton_city_name += "市"
#         city_name.append(canton_city_name)
#         city_confirm.append(canton_city_confirm)
#     return city_name,city_confirm
# city_name,city_confirm = canton_data_list_(canton_data)
# canton_data_list = list(zip(city_name,city_confirm))  # zip的作用是将多个可迭代对象（如列表、元组等）按顺序配对，生成一个新的可迭代对象
# print(canton_data_list)
for canton_city_data in canton_data:
    canton_city_name = canton_city_data['name']
    canton_city_confirm = canton_city_data['total']['confirm']
    if canton_city_name == "境外输入" or canton_city_name == "地区待确认":
        continue
    else:
        canton_city_name += "市"
    canton_data_list.append((canton_city_name,canton_city_confirm))
# 构建地图对象
map = Map()

# 添加地图数据
map.add("广东省疫情地图",canton_data_list,"广东")

# 设置全局选项
map.set_global_opts(
    title_opts=TitleOpts(is_show=True, pos_bottom="1%", pos_left="center"),
    visualmap_opts=VisualMapOpts(is_show=True,is_piecewise=True,pieces=[
        {"min":1 , "max":9, "label":"1-9", "color":"#CCFFFF"},
        {"min":10, "max":49, "label":"10-49", "color":"#FFFF99"},
        {"min":50, "max":99, "label":"50-99", "color":"#FF9966"},
        {"min":100, "max":199, "label":"100-199", "color":"#FF6666"},
        {"min":200, "max":299, "label":"200-299", "color":"#CC3333"},
        {"min":300, "label":"300+", "color":"#990033"}]))

# 绘制地图
map.render("广东省疫情地图.html")

